package org.jeecg.common.token;

import lombok.Data;
import org.jeecg.common.constant.RedisConstant;
import org.jeecg.common.exception.ValidException;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.common.util.app.CommonUtil;

@Data
public class MobileCodeToken implements ValidToken {

    private static final long serialVersionUID = 1L;
    private String code;
    private String mobile;

    public MobileCodeToken(){
        super();
    }
    public MobileCodeToken(String code, String mobile) {
        this.code = code;
        this.mobile = mobile;
    }

    @Override
    public void isValid() throws ValidException {
        String code = this.code;
        RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class);
        String redisCode = (String) redisUtil.get(RedisConstant.getLoginCodeKey(this.mobile));
        String encryptCode = CommonUtil.encryptData(String.valueOf(code),this.mobile);
        if (null == redisCode) {
            throw new ValidException("验证码失效");
        } else if (null == encryptCode || !encryptCode.equals(redisCode)) {
            throw new ValidException("验证码错误");
        }
    }

    @Override
    public Object getPrincipal() {
        return null;
    }

    @Override
    public Object getCredentials() {
        return null;
    }
}
